package com.gec.zhs.controller;

import cn.hutool.core.util.ObjectUtil;
import com.gec.zhs.annotation.ZhsLogin;
import com.gec.zhs.entity.dto.TokenUserInfoDto;
import com.gec.zhs.entity.enums.ResponseCodeEnum;
import com.gec.zhs.entity.enums.VideoStatusEnum;
import com.gec.zhs.entity.pojo.*;
import com.gec.zhs.entity.query.*;
import com.gec.zhs.entity.vo.*;
import com.gec.zhs.exception.BusinessException;
import com.gec.zhs.service.*;
import com.gec.zhs.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhs
 * @version 1.0
 * @date 2025-02-09
 * @description 用户创作中心
 */

@RestController
@Validated
@RequestMapping("/ucenter")
public class UserCenterVideoPostController extends ABaseController {

    @Autowired
    private VideoInfoPostService videoInfoPostService;

    @Autowired
    private VideoInfoFilePostService videoInfoFilePostService;

    @Autowired
    private VideoInfoService videoInfoService;

    @Autowired
    private VideoCommentService videoCommentService;

    @Autowired
    private VideoDanmuService videoDanmuService;


    /**
     * 用户投稿中心视频相关
     */

    //视频投稿
    @ApiOperation("发布视频")
    @RequestMapping("/postVideo")
    @ZhsLogin(checkLogin = true)
    public ResponseVO postVideo(String videoId,
                                String videoCover,
                                String videoName,
                                Integer pCategoryId,
                                Integer categoryId,
                                Integer postType,
                                String tags,
                                String introduction,
                                String interaction,
                                String uploadFileList) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        List<VideoInfoFilePost> fileInfoList = JsonUtils.convertJsonArrayToList(uploadFileList, VideoInfoFilePost.class);

        VideoInfoPost videoInfo = new VideoInfoPost();
        videoInfo.setVideoId(videoId);
        videoInfo.setVideoName(videoName);
        videoInfo.setVideoCover(videoCover);
        videoInfo.setPCategoryId(pCategoryId);
        videoInfo.setCategoryId(categoryId);
        videoInfo.setPostType(postType);
        videoInfo.setTags(tags);
        videoInfo.setIntroduction(introduction);
        videoInfo.setInteraction(interaction);
        videoInfo.setUserId(tokenUserInfoDto.getUserId());

        videoInfoPostService.saveVideoInfo(videoInfo, fileInfoList);
        return getSuccessResponseVO(null);
    }

    //查询视频list
    @ApiOperation("加载视频")
    @RequestMapping("/loadVideoList")
    @ZhsLogin(checkLogin = true)
    public ResponseVO loadVideoList(Integer status, String videoNameFuzzy, Integer pageNo) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        VideoInfoPostQuery videoInfoPostQuery = new VideoInfoPostQuery();
        videoInfoPostQuery.setPageNo(pageNo);
        videoInfoPostQuery.setVideoNameFuzzy(videoNameFuzzy);
        videoInfoPostQuery.setUserId(tokenUserInfoDto.getUserId());
        videoInfoPostQuery.setOrderBy("v.create_time desc");
        if (status != null) {
            if (status == -1) {
                videoInfoPostQuery.setExcludeStatusArray(new Integer[]{VideoStatusEnum.STATUS4.getStatus(), VideoStatusEnum.STATUS3.getStatus()});
            } else {
                videoInfoPostQuery.setStatus(status);
            }
        }
        videoInfoPostQuery.setQueryCountInfo(true);
        PaginationResultVO<VideoInfoPost> resultVO = videoInfoPostService.findListByPage(videoInfoPostQuery);
        return getSuccessResponseVO(resultVO);
    }

    //投稿视频数量
    @ApiOperation("获取视频数量")
    @RequestMapping("/getVideoCountInfo")
    @ZhsLogin(checkLogin = true)
    public ResponseVO getVideoCountInfo() {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        VideoInfoPostQuery videoInfoPostQuery = new VideoInfoPostQuery();
        videoInfoPostQuery.setUserId(tokenUserInfoDto.getUserId());
        videoInfoPostQuery.setStatus(VideoStatusEnum.STATUS3.getStatus());
        //审核通过的数量
        Integer auditPassCount = videoInfoPostService.findCountByParam(videoInfoPostQuery);

        //审核失败的数量
        videoInfoPostQuery.setStatus(VideoStatusEnum.STATUS4.getStatus());
        Integer auditFailCount = videoInfoPostService.findCountByParam(videoInfoPostQuery);

        videoInfoPostQuery.setStatus(null);
        //排除审核通过和审核失败的，剩下的就是审核中的视频数量
        videoInfoPostQuery.setExcludeStatusArray(new Integer[]{VideoStatusEnum.STATUS3.getStatus(),VideoStatusEnum.STATUS4.getStatus()});
        Integer auditInProgressCount = videoInfoPostService.findCountByParam(videoInfoPostQuery);

        VideoStatusCountInfoVO videoStatusCountInfoVO = new VideoStatusCountInfoVO();
        videoStatusCountInfoVO.setAuditPassCount(auditPassCount);
        videoStatusCountInfoVO.setAuditFailCount(auditFailCount);
        videoStatusCountInfoVO.setInProgress(auditInProgressCount);
        return getSuccessResponseVO(videoStatusCountInfoVO);
    }

    @ApiOperation("创作中心编辑修改投稿的视频")
    @RequestMapping("/getVideoByVideoId")
    @ZhsLogin(checkLogin = true)
    public ResponseVO getVideoByVideoId(String videoId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();

        VideoInfoPost videoInfoPost = videoInfoPostService.getVideoInfoPostByVideoId(videoId);
        if(ObjectUtil.isEmpty(videoInfoPost)||!videoInfoPost.getUserId().equals(tokenUserInfoDto.getUserId())){
            throw new BusinessException(ResponseCodeEnum.CODE_404);
        }

        //视频列表的分p
        VideoInfoFilePostQuery videoInfoFilePostQuery = new VideoInfoFilePostQuery();
        videoInfoFilePostQuery.setVideoId(videoId);
        videoInfoFilePostQuery.setOrderBy("file_index asc");
        PaginationResultVO<VideoInfoFilePost> videoInfoFilePostServiceList = videoInfoFilePostService.findListByPage(videoInfoFilePostQuery);


        VideoPostEditInfoVo videoPostEditInfoVo = new VideoPostEditInfoVo();
        videoPostEditInfoVo.setVideoInfoPost(videoInfoPost);
        videoPostEditInfoVo.setVideoInfoFilePostList(videoInfoFilePostServiceList.getList());
        return getSuccessResponseVO(videoPostEditInfoVo);
    }

    @ApiOperation("创作中心编辑修改投稿的视频")
    @RequestMapping("/saveVideoInteraction")
    @ZhsLogin(checkLogin = true)
    public ResponseVO saveVideoInteraction(String videoId,String interaction) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        videoInfoService.changeInteraction(videoId,tokenUserInfoDto.getUserId(), interaction);
        return getSuccessResponseVO(null);
    }

    @ApiOperation("投稿中心删除视频")
    @RequestMapping("/deleteVideo")
    @ZhsLogin(checkLogin = true)
    public ResponseVO delVideo(String videoId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        videoInfoService.deleteVideo(videoId,tokenUserInfoDto.getUserId());
        return getSuccessResponseVO(null);
    }


    /**
     * 用户投稿中心数据相关
     * 弹幕管理
     *
     */

    @ApiOperation("创作中心加载视频")
    @RequestMapping("/loadAllVideo")
    @ZhsLogin(checkLogin = true)
    public ResponseVO loadAllVideo() {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        VideoInfoQuery videoInfoQuery = new VideoInfoQuery();
        videoInfoQuery.setUserId(tokenUserInfoDto.getUserId());
        videoInfoQuery.setOrderBy("create_time desc");
        List<VideoInfo> VideoInfoList = videoInfoService.findListByParam(videoInfoQuery);
        return getSuccessResponseVO(VideoInfoList);
    }


    @ApiOperation("创作中心评论管理")
    @RequestMapping("/loadComment")
    @ZhsLogin(checkLogin = true)
    public ResponseVO loadComment(Integer pageNo,String videoId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();

        VideoCommentQuery videoCommentQuery = new VideoCommentQuery();
        videoCommentQuery.setUserId(tokenUserInfoDto.getUserId());
        videoCommentQuery.setVideoId(videoId);
        videoCommentQuery.setPageNo(pageNo);
        videoCommentQuery.setOrderBy("comment_id desc");
        videoCommentQuery.setQueryVideoInfo(Boolean.TRUE);

        PaginationResultVO<VideoComment> page = videoCommentService.findListByPage(videoCommentQuery);
        return getSuccessResponseVO(page);
    }

    @ApiOperation("创作中心删除评论")
    @RequestMapping("/delComment")
    @ZhsLogin(checkLogin = true)
    public ResponseVO delComment(Integer commentId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        videoCommentService.deleteComment(commentId,tokenUserInfoDto.getUserId());
        return getSuccessResponseVO(null);
    }

    /**
     * 用户投稿中心数据相关
     * 创作中心弹幕管理
     *
     */
    @ApiOperation("创作中心弹幕管理-加载弹幕")
    @RequestMapping("/loadDanmu")
    @ZhsLogin(checkLogin = true)
    public ResponseVO loadDanmu(Integer pageNo, String videoId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();

        VideoDanmuQuery videoDanmuQuery = new VideoDanmuQuery();
        videoDanmuQuery.setUserId(tokenUserInfoDto.getUserId());
        videoDanmuQuery.setPageNo(pageNo);
        videoDanmuQuery.setVideoId(videoId);
        videoDanmuQuery.setOrderBy("danmu_id desc");
        videoDanmuQuery.setQueryVideoInfo(true);

        PaginationResultVO<VideoDanmu> page = videoDanmuService.findListByPage(videoDanmuQuery);
        return getSuccessResponseVO(page);
    }


    @ApiOperation("创作中心弹幕管理-删除弹幕")
    @RequestMapping("/delDanmu")
    @ZhsLogin(checkLogin = true)
    public ResponseVO delDanmu(Integer danmuId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        videoDanmuService.deleteDanmu(tokenUserInfoDto.getUserId(),danmuId);
        return getSuccessResponseVO(null);
    }
}
